﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace down2App
{
    /// <summary>
    /// json url任务下载对象
    /// </summary>
    class JsonUrlDowner
    {
        public FileInf fileSvr = new FileInf();
        HttpDownloaderAppLib.DownloaderPartitionClass downer = null;
        public dgt_msg show_msg = null;
        public dgt_ent downComplete = null;

        public JsonUrlDowner(HttpDownloaderAppLib.DownloaderPartitionClass d)
        {
            this.downer = d;
        }

        public void down()
        {
            Thread t = new Thread(delegate () {
                this.show_msg("------开始下载json url------");
                JObject o = new JObject();
                o["name"] = "down_json";
                o["id"] = this.fileSvr.id;
                //自定义文件名称
                if (!string.IsNullOrEmpty(this.fileSvr.nameLoc))
                {
                    o["nameLoc"] = this.fileSvr.nameLoc;
                }
                o["fileUrl"] = this.fileSvr.fileUrl;
                o["folderLoc"] = this.fileSvr.folderLoc;
                o["pathLoc"] = this.fileSvr.pathLoc;
                this.downer.postMessage(o.ToString());
            });
            t.Start();
        }
        public void stop()
        {
            JObject o = new JObject();
            o["name"] = "stop_file";
            o["id"] = this.fileSvr.id;
            this.downer.postMessage(o.ToString());
        }

        public void openFile()
        {
            JObject o = new JObject();
            o["name"] = "open_file";
            o["pathLoc"] = this.fileSvr.pathLoc;
            this.downer.postMessage(o.ToString());
        }

        public void openPath()
        {
            JObject o = new JObject();
            o["name"] = "open_path";
            o["pathLoc"] = this.fileSvr.pathLoc;
            this.downer.postMessage(o.ToString());
        }

        public void down_complete(JToken o)
        {
            this.show_msg("------json url下载完毕------");
            this.show_msg(this.fileSvr.pathLoc);
            this.downComplete(this.fileSvr.id);
        }

        public void down_recv_size(JToken o)
        {
            this.show_msg("------接收远程大小------");
            this.fileSvr.sizeSvr = o["size"].ToString();
            this.fileSvr.lenSvr = long.Parse(o["len"].ToString());
            this.show_msg(this.fileSvr.sizeSvr);
        }
        public void down_recv_name(JToken o)
        {
            this.show_msg("------接收文件名称------");
            this.fileSvr.nameSvr = o["nameSvr"].ToString();
            this.fileSvr.pathLoc = o["pathLoc"].ToString();
            if (string.IsNullOrEmpty(this.fileSvr.nameLoc))
                this.fileSvr.nameLoc = o["nameSvr"].ToString();
            this.show_msg(this.fileSvr.nameSvr);
        }
        public void query_end(JToken o)
        {
            this.show_msg("------查询结束------");
            this.fileSvr.sizeSvr = o["sizeSvr"].ToString();
            this.fileSvr.lenSvr = long.Parse(o["lenSvr"].ToString());
            this.fileSvr.pathLoc = o["pathLoc"].ToString();
            this.fileSvr.nameLoc = o["nameLoc"].ToString();
        }
        public void down_process(JToken o)
        {
            this.show_msg("------下载进度------");
            this.fileSvr.lenLoc = long.Parse(o["lenLoc"].ToString());
            this.fileSvr.perLoc = o["percent"].ToString();
            this.fileSvr.sizeSvr = o["lenRecv"].ToString();

            var msg = string.Format("已下载大小：{0}/{1} {2} {3}",
                this.fileSvr.lenLoc,
                this.fileSvr.lenSvr,
                this.fileSvr.perLoc,
                o["time"].ToString()
                );
            this.show_msg(msg);
        }

        public void json_url_load(JToken o) { 
            this.show_msg("------正在拉取数据------");
        }
        public void json_url_init(JToken o) { 
            this.show_msg("------数据拉取成功，正在解析数据------");
        }
        public void json_url_err(JToken o) { 
            this.show_msg("------数据加载错误------");
            var id = o["id"].ToString();
            var code = o["code"].ToString();
        }
        public void json_url_cmp(JToken o) { 
            this.show_msg("------数据解析成功------");
            this.show_msg("名称:" + o["nameLoc"].ToString());
            this.show_msg("大小:" + o["sizeSvr"].ToString());
        }

        public void down_begin(JToken o)
        {
            this.show_msg("------json url开始下载------");
        }

        public void down_error(JToken o)
        {
            this.show_msg("------json url下载错误------");
        }

        public void down_stoped(JToken o)
        {
            this.show_msg("------json url下载停止------");
        }
    }
}
